package com.yt.user.service;

import com.yt.common.utils.Md5Utils;
import com.yt.user.models.bo.UserRegisterSaveBO;
import com.yt.user.models.dto.UserLoginDTO;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import javax.annotation.Resource;
import com.yt.user.common.config.YmlConfig;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.yt.user.mapper.UserInfoMapper;
import com.yt.user.models.service.IUserInfoService;
import com.yt.user.models.bo.UserInfoSaveBO;
import com.yt.user.models.dto.UserInfoDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.transaction.annotation.Transactional;

/**
 * 用户详情控制器
 *
 * @author lvzeming
 * @Date 2025-08-20 12:11:57
 */
@DubboService
public class UserInfoServiceImpl extends ServiceImpl<UserInfoMapper, UserInfoDTO> implements IUserInfoService{

    @Resource
    private YmlConfig ymlConfig;

    @Autowired
    @Lazy
    private UserLoginServiceImpl iUserLoginService;

    @Override
    public boolean add(UserInfoSaveBO saveBO) {
        UserInfoDTO dto = new UserInfoDTO();
        BeanUtils.copyProperties(saveBO,dto);
        Long id = ymlConfig.getPkId();
        dto.setId(id);
        return this.insert(dto);
    }

    @Override
    @Transactional
    public UserInfoDTO saveUserInfo(UserRegisterSaveBO userRegisterSaveBO) {
        //保存用户信息
        UserInfoDTO dto = new UserInfoDTO();
        BeanUtils.copyProperties(userRegisterSaveBO,dto);
        Long id = ymlConfig.getPkId();
        dto.setId(id);
        this.insert(dto);

        //保存登录信息
        UserLoginDTO userLoginDTO = new UserLoginDTO();
        BeanUtils.copyProperties(userRegisterSaveBO,userLoginDTO);
        Long loginId = ymlConfig.getPkId();
        userLoginDTO.setId(loginId);
        String salt = Md5Utils.createSalt();
        userLoginDTO.setSalt(salt);
        //对密码进行加密
        userLoginDTO.setPassword(Md5Utils.MD5Encode(userRegisterSaveBO.getPassword(),salt));
        //保存用户id
        userLoginDTO.setUserId(id);
        userLoginDTO.setLoginName(userRegisterSaveBO.getMobile());
        iUserLoginService.insert(userLoginDTO);

        return dto;
    }


}
